# Maintainer: Carlos Antunes <cmantunes@gmail.com>
# Maintainer: fauxpark <fauxpark@gmail.com>
# Contributed by: nanodude

_realname=newlib
_target=arm-none-eabi
_gccver=8.4.0

pkgbase=mingw-w64-${_target}-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_target}-${_realname}")
pkgver=3.3.0
pkgrel=1
pkgdesc='GNU Tools for ARM Embedded Processors - Newlib (mingw-w64)'
arch=('any')
url="https://sourceware.org/newlib/"
license=('Various')
groups=("${MINGW_PACKAGE_PREFIX}-${_target}-toolchain")
depends=("${MINGW_PACKAGE_PREFIX}-${_target}-binutils")
options=('staticlibs' '!strip' '!emptydirs')
source=(
    "https://sourceware.org/pub/newlib/newlib-${pkgver}.tar.gz"
    "https://ftp.gnu.org/gnu/gcc/gcc-${_gccver}/gcc-${_gccver}.tar.xz"
)
sha256sums=(
    '58dd9e3eaedf519360d92d84205c3deef0b3fc286685d1c562e245914ef72c66'
    'e30a6e52d10e1f27ed55104ad233c30bd1e99cfb5ff98ab022dc941edd1b2dd4'
)
noextract=("newlib-${pkgver}.tar.gz")

prepare() {
    cd ${srcdir}
    # bsdtar doesn't like the symlinks
    tar -xf newlib-${pkgver}.tar.gz

    cd ${srcdir}/gcc-${_gccver}
    mkdir gcc-build-${MINGW_CHOST}
    mkdir gcc-install-${MINGW_CHOST}

    cd ${srcdir}/newlib-${pkgver}
    mkdir {newlib,nano}-build-${MINGW_CHOST}
}

# Newlib is required to build GCC, but GCC is also required to compile newlib
# So we must build a minimal GCC first, to avoid the circular dependency
_build_gcc() {
    cd ${srcdir}/gcc-${_gccver}/gcc-build-${MINGW_CHOST}

    cp -a ${MINGW_PREFIX}/${_target} ${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}

    local _GCC_LDFLAGS="${LDFLAGS} -Wl,--disable-dynamicbase"
    if [ "${CARCH}" = 'x86_64' ]; then
        _GCC_LDFLAGS+=",--image-base=0x400000"
    fi
    ../configure \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --prefix=/ \
        --target=${_target} \
        --enable-languages=c \
        --disable-nls \
        --disable-libssp \
        --disable-shared \
        --disable-threads \
        --disable-libgomp \
        --disable-werror \
        --disable-lto \
        --disable-libffi \
        --disable-decimal-float \
        --disable-libmudflap \
        --disable-libquadmath \
        --disable-libstdcxx-pch \
        --disable-libsanitizer \
        --disable-tls \
        --enable-multilib \
        --enable-interwork \
        --without-headers \
        --with-libelf \
        --with-newlib \
        --with-multilib-list=rmprofile \
        --with-host-libstdcxx="-static-libgcc -Wl,-Bstatic,-lstdc++,-Bdynamic -lm" \
        --with-sysroot=${MINGW_PREFIX} \
        --with-build-sysroot=${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST} \
        --with-build-time-tools=${MINGW_PREFIX}/${_target}/bin \
        LDFLAGS="${_GCC_LDFLAGS}"

    make all-gcc
    make DESTDIR=${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST} install-gcc
}

build() {
    _build_gcc

    cd ${srcdir}/newlib-${pkgver}/newlib-build-${MINGW_CHOST}

    ../configure \
        PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}" \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${_target} \
        --prefix=${MINGW_PREFIX} \
        --enable-newlib-io-long-long \
        --enable-newlib-io-c99-formats \
        --enable-newlib-register-fini \
        --enable-newlib-retargetable-locking \
        --disable-newlib-supplied-syscalls \
        --disable-nls
    make PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}"

    cd ${srcdir}/newlib-${pkgver}/nano-build-${MINGW_CHOST}

    ../configure \
        PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}" \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${_target} \
        --prefix=${MINGW_PREFIX} \
        --disable-newlib-supplied-syscalls \
        --enable-newlib-reent-small \
        --enable-newlib-retargetable-locking \
        --disable-newlib-fvwrite-in-streamio \
        --disable-newlib-fseek-optimization \
        --disable-newlib-wide-orient \
        --enable-newlib-nano-malloc \
        --disable-newlib-unbuf-stream-opt \
        --enable-lite-exit \
        --enable-newlib-global-atexit \
        --enable-newlib-nano-formatted-io \
        --disable-nls
    make PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}"
}

package() {
    cd ${pkgdir}

    # make install complains if theses dirs do not already exist, for some reason
    mkdir -p ${pkgdir}${MINGW_PREFIX}/${_target}/lib/arm/v5te/{hard,softfp}
    mkdir -p ${pkgdir}${MINGW_PREFIX}/${_target}/lib/thumb/nofp
    mkdir -p ${pkgdir}${MINGW_PREFIX}/${_target}/lib/thumb/v{6-m,7,7-m,7e-m,8-m.base,8-m.main}/nofp
    mkdir -p ${pkgdir}${MINGW_PREFIX}/${_target}/lib/thumb/v{7+fp,7e-m+fp,7e-m+dp,8-m.main+fp,8-m.main+dp}/{hard,softfp}

    cd ${srcdir}/newlib-${pkgver}/nano-build-${MINGW_CHOST}

    make DESTDIR=${pkgdir} PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}" install

    find ${pkgdir} -regex ".*/lib\(c\|g\|rdimon\)\.a" -exec rename .a _nano.a '{}' \;
    install -Dm644 -t ${pkgdir}${MINGW_PREFIX}/${_target}/include/newlib-nano ${pkgdir}${MINGW_PREFIX}/${_target}/include/newlib.h

    cd ${srcdir}/newlib-${pkgver}/newlib-build-${MINGW_CHOST}

    make DESTDIR=${pkgdir} PATH="${srcdir}/gcc-${_gccver}/gcc-install-${MINGW_CHOST}/bin:${PATH}" install

    find ${pkgdir}${MINGW_PREFIX}/${_target}/lib \( -name "*.a" -or -name "*.o" \) -exec ${_target}-objcopy -R .comment -R .note -R .debug_info -R .debug_aranges -R .debug_pubnames -R .debug_pubtypes -R .debug_abbrev -R .debug_line -R .debug_str -R .debug_ranges -R .debug_loc '{}' \;
}
